package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.GroupChatActivityAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatActivity;
import com.bnyy.message.bean.GroupChatActivityDetail;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/6
 **/
public class GroupChatActivitiesActivity extends BaseActivityImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int groupChatId;

    public static void show(Context context, int groupChatId) {
        Intent intent = new Intent(context, GroupChatActivitiesActivity.class);
        intent.putExtra("groupChatId", groupChatId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return "群活动";
    }

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getGroupChatActivities();
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getGroupChatActivities();
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groupChatId = getIntent().getIntExtra("groupChatId", 0);

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == RESULT_OK) {
                    mRefreshLayout.autoRefresh();
                }
            }
        });

        TextView tvOption = new TextView(mContext);
        tvOption.setTextAppearance(R.style.NormalTextView);
        tvOption.setText("发布活动");
        tvOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GroupChatActivityCreateActivity.show(mContext, groupChatId, launcher);
            }
        });
        setOptions(tvOption);

        tvNoData.setText("暂无活动开展，快去发起活动吧~");

        int pd12 = SizeUtils.dp2px(12);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(pd12, pd12, pd12, pd12);

        TextView tvCreatGroupChatActivity = new TextView(mContext);
        GradientDrawable bg = new GradientDrawable();
        bg.setCornerRadius(360);
        bg.setColor(ContextCompat.getColor(mContext, R.color.green_theme));
        tvCreatGroupChatActivity.setBackground(bg);

        tvCreatGroupChatActivity.setGravity(Gravity.CENTER);
        tvCreatGroupChatActivity.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        tvCreatGroupChatActivity.setPadding(pd12 * 3, pd12, pd12 * 3, pd12);
        tvCreatGroupChatActivity.setTextColor(Color.WHITE);
        tvCreatGroupChatActivity.setText("发布活动");
        tvCreatGroupChatActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvOption.performClick();
            }
        });
        llNoData.addView(tvCreatGroupChatActivity, layoutParams);

        ActivityResultLauncher<Intent> joinActivityLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getData() != null) {
                            GroupChatActivityDetail groupChatActivityDetail = (GroupChatActivityDetail) result.getData().getSerializableExtra("groupChatActivityDetail");

                            if (groupChatActivityDetail != null) {
                                int activityId = groupChatActivityDetail.getActivity_id();

                                for (int i = 0; i < adapter.getDatas().size(); i++) {
                                    GroupChatActivity groupChatActivity = adapter.getDatas().get(i);
                                    if (groupChatActivity.getActivity_id() == activityId) {
                                        groupChatActivity.setNumber(groupChatActivityDetail.getNumber());
                                        groupChatActivity.setJoined(groupChatActivityDetail.isJoined());
                                        adapter.notifyItemChanged(i);
                                    }
                                }
                            }
                        }
                    }
                });
        adapter = new GroupChatActivityAdapter(mContext, new Consumer<GroupChatActivity>() {
            @Override
            public void accept(GroupChatActivity groupChatActivity) {
                GroupChatActivityDetailActivity.show(mContext, groupChatActivity.getActivity_id(), joinActivityLauncher);
            }
        });

        int padding = SizeUtils.dp2px(12);
        recyclerView.setPadding(padding, 0, padding, padding);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);

        mRefreshLayout.autoRefresh();
    }

    private int size = 20;
    private int page = 1;
    private GroupChatActivityAdapter adapter;

    private void getGroupChatActivities() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", groupChatId);
        params.put("page_index", page);
        params.put("page_size", size);

        requestManager.request(requestManager.mMessageRetrofitService.getGroupChatActivities(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<GroupChatActivity>>(mRefreshLayout) {
                    @Override
                    public void onSuccess(ArrayList<GroupChatActivity> groupChatActivities) {
                        super.onSuccess(groupChatActivities);

                        mRefreshLayout.setEnableLoadMore(groupChatActivities.size() == size);

                        if (page == 1) {
                            adapter.refresh(groupChatActivities);

                            if (groupChatActivities.size() > 0) {
                                llNoData.setVisibility(View.GONE);
                            } else {
                                llNoData.setVisibility(View.VISIBLE);
                            }
                        } else {
                            adapter.loadMore(groupChatActivities);
                        }
                    }
                });
    }
}










































